package com.googlecode.wmlynar.j2meunitgenerator;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import com.thoughtworks.qdox.JavaDocBuilder;

/**
 * Main entry class. Parses parameters, uses test scanner and generator classes to generate
 * test suite class.
 *
 * @author Wojciech Mlynarczyk (woj)
 *
 */
public class TestsScanner {

	public static void main(String[] args) throws IOException {

//		String testsDirectory = "src/testme/java";
//		String outputDirectory = "src/testme/java";
//		String generatedFullClassName = "com.tomtom.me.testme.AllTestsSuite";

		HashMap<String, String> map = new HashMap<String, String>();

		for(int i=0; i<args.length; i+=2) {
			String name = args[i];
			if(!name.startsWith("--")) {
				help();
			}
			name = name.substring(2);
			if((i + 1) < args.length && !args[i+1].startsWith("--")) {
				map.put(name, args[i+1]);
			} else {
				help();
			}
		}

		String testsDirectory = map.get("testsDir");
		String outputDirectory = map.get("outputDir");
		String generatedFullClassName = map.get("generatedClassName");

		if(testsDirectory==null || outputDirectory==null || generatedFullClassName==null) {
			help();
		}

		generateFile(testsDirectory, outputDirectory, generatedFullClassName);
	}

	private static void help() {
		System.out.println("usage: j2meunit-generator --testsDir dir --outputDir dir --generatedClassName name");
		System.exit(1);
	}

	public static void generateFile(String testsDirectory,
			String outputDirectory, String generatedFullClassName)
			throws IOException {

	    System.out.println("Generating J2ME unit test suites");
		System.out.println("tests directory: " + testsDirectory);
		System.out.println("output directory: " + outputDirectory);

		String generatedPackageName;
		String generatedClassName;

		int idx = generatedFullClassName.lastIndexOf('.');
		if(idx>0) {
			generatedPackageName = generatedFullClassName.substring(0, idx);
			generatedClassName = generatedFullClassName.substring(idx+1);
		} else {
			generatedPackageName = "";
			generatedClassName = generatedFullClassName;
		}

		System.out.println("generated class package name: " + generatedPackageName);
		System.out.println("generated class name: " + generatedClassName);

		JavaDocBuilder builder = new JavaDocBuilder();
		builder.addSourceTree(new File(testsDirectory));

		TestClassCollector collector = new TestClassCollector();
		ArrayList<TestClass> testClasses = collector.collect(builder);

		// sort all test classes and test methods alphabetically
        Collections.sort(testClasses, new Comparator<TestClass>() {
            public int compare(TestClass o1, TestClass o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        for (TestClass clas : testClasses) {
            Collections.sort(clas.getTestMethods());
        }

		TestSuiteGenerator generator = new TestSuiteGenerator();

		String content = generator.generate(
				generatedPackageName,
				generatedClassName,
				testClasses);

		//System.out.println(content);

		new FileDestination().write(generatedClassName, generatedPackageName, outputDirectory, content);
		System.out.println();
	}

}
